package com.example.day30_music_synthetic;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import java.io.File;
import java.io.IOException;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    MusicProcess musicProcess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission();
        musicProcess = new MusicProcess();
    }

    public boolean checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, 1);

        }
        return false;
    }

    public void clip(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final String input1Path = new File(Environment.getExternalStorageDirectory(), "music.mp3").getAbsolutePath();
                final String input2Path = new File(Environment.getExternalStorageDirectory(), "input4.mp4").getAbsolutePath();
                final String outPath = new File(Environment.getExternalStorageDirectory(), "out.mp3").getAbsolutePath();
                try {
                    FileUtils.copyAssets(getApplicationContext(),"input1.mp3",input1Path);
                    FileUtils.copyAssets(getApplicationContext(),"input2.mp4",input2Path);
                    musicProcess.mixAudioTrack(MainActivity.this,input2Path,input1Path,outPath,10*1000*1000,15*1000*1000,
                            10,100);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
